package com.daqsoft.controller;

import com.daqsoft.data.Message;
import com.daqsoft.data.User;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.NoticeService;
import com.daqsoft.service.UserService;
import com.daqsoft.socket.UserWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.ServerEndpoint;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户通讯
 *
 * @author 周麟东 on 13:54
 */
@RequestMapping("user")
@RestController
@Verify(desc = "用户控制器")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ServerProperties serverProperties;

    @Value("${local.host}")
    private String host;

    /**
     * 玩家登陆
     *
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping("login")
    @Verify(value = VerifyType.EVERYONE , desc = "用户登录")
    public GeneralResultEnum login(HttpServletRequest request , String username, String password) {
        User user = userService.login(username,password);
        if (user == null){
            return GeneralResultEnum.FAIL.setDesc("用户名或密码错误");
        }
        request.getSession().setAttribute("user",user);
        return getUserInfo(request);
    }

    /**
     * 获取用户拥有的模块
     * @param request 请求
     * @return 模块
     */
    @RequestMapping("getMenu")
    @Verify(value = VerifyType.NEED_LOGIN , desc = "获取模块列表")
    public GeneralResultEnum getModules(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        return GeneralResultEnum.SUCCESS.setData(userService.getUserModules(user));
    }

    @RequestMapping("getUserInfo")
    @Verify(value = VerifyType.NEED_LOGIN , desc = "获取自己的信息")
    public GeneralResultEnum getUserInfo(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Map<String , Object> userInfo = new HashMap<>();
        userInfo.put("realName",user.getRealName());
        userInfo.put("username",user.getUsername());
        userInfo.put("id",user.getId());
        userInfo.put("groupId",user.getUserGroup().getId());
        userInfo.put("groupName",user.getUserGroup().getGroupName());
        return GeneralResultEnum.SUCCESS.setData(userInfo);
    }
    @RequestMapping("logout")
    @Verify(value = VerifyType.NEED_LOGIN , desc = "登出")
    public GeneralResultEnum logout(HttpServletRequest request){
        request.getSession().invalidate();
        return GeneralResultEnum.SUCCESS.setDesc("登出成功");
    }

    @RequestMapping("getMessage")
    @Verify(value = VerifyType.NEED_LOGIN , desc = "获取用户消息")
    public GeneralResultEnum getMessage(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<Message> messages = noticeService.getFromUserId(user.getId());
        if (messages == null){
            return GeneralResultEnum.FAIL.setDesc("无消息");
        }
        List<Map<String,Object>> list = new ArrayList<>();
        for (Message message : messages) {
            list.add(message.toMap());
            noticeService.removeMessage(user.getId());
        }
        return GeneralResultEnum.SUCCESS.setData(list);
    }
    @RequestMapping("getWebSocketInfo")
    @Verify(value = VerifyType.NEED_LOGIN , desc = "获取用户消息")
    public GeneralResultEnum getWebSocketInfo(HttpServletRequest request) throws UnknownHostException {
        //获取本机IP地址
        InetAddress address = InetAddress.getLocalHost();
        String host = address.getHostAddress();
        if (host.equals("127.0.0.1") || host.equals("localhost")){
            host = this.host;
        }
        //组装
        host = "ws://"+host+":"+serverProperties.getPort()+serverProperties.getContextPath()+UserWebSocket.class.getAnnotation(ServerEndpoint.class).value();
        return GeneralResultEnum.SUCCESS.setData(host);
    }
}
